<template>
  <div class="loginContentBox">
    <img class="langSwitchBtn" src="@/assets/img/img85.png" mode="" @click="onChangeLangKey()" />
    <div class="loginContentBox_left">
      <div class="loginContentBox_left_text1">{{$t('message.Changyang_digital_energy_carbon_management_platform')}}</div>
      <div class="loginContentBox_left_text2">{{$t('message.tip1')}}</div>
    </div>
    <div class="loginContentBox_right">
      <div class="loginContentBox_right_text1">{{$t('message.Welcome_to_use')}}</div>
      <a-form-model class="user-layout-login" @keyup.enter.native="handleSubmit">
        <a-tabs :activeKey="customActiveKey" :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
                @change="handleTabClick">
          <a-tab-pane key="tab1" :tab="$t('message.Account_login')">
            <login-account ref="alogin" @validateFail="validateFail" @success="requestSuccess"
                           @fail="requestFailed"></login-account>
          </a-tab-pane>

          <a-tab-pane key="tab2" :tab="$t('message.Phone_login')">
            <login-phone ref="plogin" @validateFail="validateFail" @success="requestSuccess"
                         @fail="requestFailed"></login-phone>
          </a-tab-pane>
        </a-tabs>
        <div class="m_cbFlex">
          <a-checkbox @change="handleRememberMeChange" default-checked>{{$t('message.Remember_account')}}</a-checkbox>
          <router-link :to="{ name: 'alteration'}" class="forge-password" style="float: right;">
            {{$t('message.Forget_password')}}
          </router-link>
        </div>
        <div style="margin-top:34px">
          <a-button size="large" type="primary" htmlType="submit" class="login-button" :loading="loginBtn"
                    @click.stop.prevent="handleSubmit" :disabled="loginBtn"> {{$t('message.Confirm')}}
          </a-button>
        </div>

      </a-form-model>
    </div>

    <div class="footerBox">

      <div class="footer">
        <div class="copyright">
          Copyright
          <a-icon type="copyright"/>
          2024 
          <!-- <span>洛阳畅阳网络科技有限公司出品</span> -->
          <span>{{$t('message.produced_by_luoyang_changyang_network_technology_co_ltd')}}</span>
        </div>
      </div>
    </div>


    <two-step-captcha v-if="requiredTwoStepCaptcha" :visible="stepCaptchaVisible" @success="stepCaptchaSuccess"
                      @cancel="stepCaptchaCancel"></two-step-captcha>
    <login-select-tenant ref="loginSelect" @success="loginSelectOk"></login-select-tenant>
    <!-- <third-login ref="thirdLogin"></third-login> -->
  </div>
</template>

<script>
import Vue from 'vue'
import {ACCESS_TOKEN, ENCRYPTED_STRING} from '@/store/mutation-types'
import ThirdLogin from './third/ThirdLogin'
import LoginSelectTenant from './LoginSelectTenant'
import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
import {getEncryptedString} from '@/utils/encryption/aesEncrypt'
import {timeFix} from '@/utils/util'

import LoginAccount from './LoginAccount'
import LoginPhone from './LoginPhone'

import {
  get_langKey,
  set_langKey,
} from "@/common/storage";
export default {
  components: {
    LoginSelectTenant,
    TwoStepCaptcha,
    ThirdLogin,
    LoginAccount,
    LoginPhone,
  },
  data() {
    return {
      customActiveKey: 'tab1',
      rememberMe: true,
      loginBtn: false,
      requiredTwoStepCaptcha: false,
      stepCaptchaVisible: false,
      encryptedString: {
        key: "",
        iv: "",
      },
    }
  },
  created() {
    Vue.ls.remove(ACCESS_TOKEN)
    this.getRouterData();
    this.rememberMe = true
  },
  methods: {
		/**
		 * 切换语言
		 */
		onChangeLangKey() {
			console.log('onChangeLangKey');
      let langKey = get_langKey() || 'zh_cn' // 获取语言
      if(langKey == 'zh_cn'){
        langKey = 'en_us'
      }else{
        langKey = 'zh_cn'
      }
			set_langKey(langKey)
      this.$i18n.locale = langKey;
      this.$router.go(0); // 刷新页面
		},
    handleTabClick(key) {
      this.customActiveKey = key
    },
    handleRememberMeChange(e) {
      this.rememberMe = e.target.checked
    },
    /**跳转到登录页面的参数-账号获取*/
    getRouterData() {
      this.$nextTick(() => {
        let temp = this.$route.params.username || this.$route.query.username || ''
        if (temp) {
          this.$refs.alogin.acceptUsername(temp)
        }
      })
    },

    //登录
    handleSubmit() {
      this.loginBtn = true;
      if (this.customActiveKey === 'tab1') {
        // 使用账户密码登录
        this.$refs.alogin.handleLogin(this.rememberMe)
      } else {
        //手机号码登录
        this.$refs.plogin.handleLogin(this.rememberMe)
      }
    },
    // 校验失败
    validateFail() {
      this.loginBtn = false;
    },
    // 登录后台成功
    requestSuccess(loginResult) {
      // this.$refs.loginSelect.show(loginResult)
      this.loginSuccess()
    },
    //登录后台失败
    requestFailed(err) {
      let description = ((err.response || {}).data || {}).message || err.message || "请求出现错误，请稍后再试"
      this.$notification['error']({
        message: '登录失败',
        description: description,
        duration: 4,
      });
      //账户密码登录错误后更新验证码
      if (this.customActiveKey === 'tab1' && description.indexOf('密码错误') > 0) {
        this.$refs.alogin.handleChangeCheckCode()
      }
      this.loginBtn = false;
    },
    loginSelectOk() {
      this.loginSuccess()
    },
    //登录成功
    loginSuccess() {
      this.$router.push({path: "/dashboard/analysis"}).catch(() => {
        console.log('登录跳转首页出错,确认首页是否存在，名称和地址是否正确')
      })
      this.$notification.success({
        // message: '欢迎',
        // description: `${timeFix()}，欢迎回来`,
        message: this.$t('message.Welcome'),
        description: this.$t('message.welcome_back'),
      });
    },

    stepCaptchaSuccess() {
      this.loginSuccess()
    },
    stepCaptchaCancel() {
      this.Logout().then(() => {
        this.loginBtn = false
        this.stepCaptchaVisible = false
      })
    },
    //获取密码加密规则
    getEncrypte() {
      var encryptedString = Vue.ls.get(ENCRYPTED_STRING);
      if (encryptedString == null) {
        getEncryptedString().then((data) => {
          this.encryptedString = data
        });
      } else {
        this.encryptedString = encryptedString;
      }
    }

  }

}
</script>
<style lang="less" scoped>
.footerBox {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  z-index: 2;

  .footer {
    padding: 0 16px;
    margin: 48px 0 24px;
    text-align: center;

    .copyright {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8px;
      color: rgba(255, 255, 255, .45);
      font-size: 14px;
    }

    .links {
      a{
        color: #0FB9A5;
      }

      a:hover {
        color: rgba(255, 255, 255, .65);
      }
    }
  }

}


.loginContentBox {
  box-sizing: border-box;
  padding: 0 233px 0 148px;
  width: 100%;
  height: 100%;
  //background: #4E4D48 url(~@/assets/background.svg) no-repeat 50%;
  background: url(~@/assets/loginBg.png) no-repeat 50%;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 30px;

  .loginContentBox_left {
    flex: 1;
  }

  .loginContentBox_left_text1 {
    font-family: HONOR Sans CN, HONOR Sans CN;
    font-weight: 800;
    font-size: 68px;
    color: #FFFFFF;
    line-height: 68px;
  }

  .loginContentBox_left_text2 {
    margin-top: 16px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 28px;
    color: #FFFFFF;
    line-height: 68px;
  }

  .loginContentBox_right {
    box-sizing: border-box;
    padding: 33px 42px 45px;
    width: 401px;
    //height: 538px;
    border-radius: 4px;
    background: #FFFFFF;
  }

  .loginContentBox_right_text1 {
    font-family: PingFang SC, PingFang SC;
    font-weight: 600;
    font-size: 28px;
    color: #333333;
    line-height: 44px;
  }

  .loginContentBox_right_text2 {
    margin-bottom: 20px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 600;
    font-size: 28px;
    color: #333333;
    line-height: 44px;
  }
}

/deep/ .ant-tabs-bar {
  margin: 0 0 34px 0;
}

/deep/ .ant-tabs-nav-scroll {
  text-align: left;
}

/deep/ .ant-tabs-nav .ant-tabs-tab {
  margin: 0 50px 0 0;
  padding: 12px 0;
}

.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(255, 255, 255, .2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color .3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}


.langSwitchBtn {
		cursor: pointer;
		position: fixed;
		z-index: 10;
		right: 30px;
		top: 30px;
    width:20px;
    height: 20px;
	}
</style>
<style>
.valid-error .ant-select-selection__placeholder {
  color: #f5222d;
}
</style>
